package com.itboye.lottery.activity.user

import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.AdapterView
import android.widget.LinearLayout
import android.widget.PopupWindow
import com.handmark.pulltorefresh.library.ObservableScrollView
import com.handmark.pulltorefresh.library.PullToRefreshBase
import com.itboye.bylibrary.listener.MyClickListener
import com.itboye.lottery.R
import com.itboye.lottery.adapter.MingXiAdapter
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.bean.JinEDetail
import com.itboye.lottery.popupwindow.MyLotteryMenuPopUpWindow
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import kotlinx.android.synthetic.main.activity_chong_zhi_ming_xi_list.*

class ChongZhi_TiXianMingXi_ZhangHuMingXi_ListActivity : BaseActivity(), ResponseCallback {
    private var jinEDetailArrayList: ArrayList<JinEDetail> = ArrayList()

    override fun success(o: Any, requestSuccessType: String) {
        pull_to_refresh_scrollView.onRefreshComplete()
        if (requestSuccessType == userPresenter.mingxi_SUCCESS) {
            if (pageIndex == 1) jinEDetailArrayList.clear()
            try {
                jinEDetailArrayList.addAll(o as ArrayList<JinEDetail>)
                adapter!!.notifyDataSetChanged()
            } catch (e: Exception) {

            }
            when (jinEDetailArrayList.size <= 0) {
                true -> {
                    ByAlert.alert("暂无数据")
                }
                else -> {
                }
            }
        }
    }


    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        pull_to_refresh_scrollView.onRefreshComplete()
        if (requestErrorType == userPresenter.mingxi_FAIL) {
            ByAlert.alert(msg)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_chong_zhi_ming_xi_list
    }

    var isClickTitle: Boolean = false
    private var type: Int = 0
    var page = 0
    var lay_transroot: LinearLayout? = null
    var userPresenter: UserPresenter = UserPresenter(this)
    private var adapter: MingXiAdapter? = null

    private var pageIndex: Int = 1

    override fun initData() {
        type = intent.getIntExtra("type", 1)//1,全部 3 充值 4 提现
        page = intent.getIntExtra("page", 0)//头部标题选中项
        initPoupwindow()
        rv_chognzhi_mingxi_list.layoutManager = LinearLayoutManager(this)
        rv_chognzhi_mingxi_list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        adapter = MingXiAdapter(this, jinEDetailArrayList)
        pull_to_refresh_scrollView.mode = PullToRefreshBase.Mode.BOTH
        pull_to_refresh_scrollView.setOnRefreshListener(object : PullToRefreshBase.OnRefreshListener2<ObservableScrollView> {
            override fun onPullDownToRefresh(refreshView: PullToRefreshBase<ObservableScrollView>) {
                pageIndex = 1
                getData()
            }

            override fun onPullUpToRefresh(refreshView: PullToRefreshBase<ObservableScrollView>) {
                pageIndex++
                getData()
            }
        })
        rv_chognzhi_mingxi_list.isNestedScrollingEnabled = false
        rv_chognzhi_mingxi_list.adapter = adapter
        setTitle(names!![page], MyClickListener {
            setTitleRightDrawble(when (isClickTitle) {
                true -> {
                    R.mipmap.trangle_down
                }
                false -> {
                    R.mipmap.trangle_up
                }
            })
            isClickTitle = !isClickTitle
            popUpWindow!!.showMyAsDropDown(lay_transroot)
        })
        getData()
    }

    private fun getData() {
        userPresenter.mingxi(type, pageIndex)
    }

    private var names: Array<out String>? = null

    private lateinit var popUpWindow: MyLotteryMenuPopUpWindow

    private fun initPoupwindow() {
        names = resources.getStringArray(
                R.array.mingxi_list)
        val types = resources.getStringArray(R.array.mingxi_list)
        popUpWindow = MyLotteryMenuPopUpWindow(this, names, types, page, AdapterView.OnItemClickListener { parent, view, position, id ->
            //            closeLoadingLayout()
            when (position) {
                0 -> {
                    type = 1
                }
                1 -> {
                    type = 3
                }
                2 -> {
                    type = 4
                }
                else -> {
                }
            }
            setTitle(types[position])
            popUpWindow.setPager(position)
            popUpWindow.notifyDataSetChanged()
//            changedText()
            if (popUpWindow.isShowing) {
                popUpWindow.dismiss()
            }
            pageIndex = 1
            getData()
        }, PopupWindow.OnDismissListener {
            isClickTitle = !isClickTitle
            setTitleRightDrawble(R.mipmap.trangle_down)
        })
    }

    override fun onClick(p0: View?) {
    }
}
